﻿import QtQuick 2.0
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.12
Rectangle {
    id: root
    anchors.fill: parent
    function setData(data){
        logModel.clear()
        for(let i = data.length-1; i >=0; i --){
            logModel.append({
                                "Item1": data[i][0],
                                "Item2": data[i][1],
                                "Item3": data[i][2],
                                "Item4": data[i][3],
                            })
        }
    }


//    // 标题
//    Rectangle{
//        height: 40*dp
//        width: root.width
//        Text{
//            text: "实验日志"
//            font.bold: true
//            font.pixelSize: config.fontsize + 6
//            anchors.verticalCenter: parent.verticalCenter
//        }
//    }

    // 表头
    Rectangle{
        y: 45*dp
        height: 40*dp
        width: root.width
        color: "#E6E6E6"
        Rectangle{              // 表格线
            color: "white"
            height: parent.height - 2
            width: parent.width
            y: 1; x: 0
        }
        Row{
            anchors.fill: parent
            Item{
                height: parent.height
                width: parent.width/6*2
                Text{
                    text: "时间"
                    anchors.centerIn: parent
                    color: "#888888"
                    font.pixelSize: config.fontsize
                }
            }Item{
                height: parent.height
                width: parent.width/6
                Text{
                    text: "动作"
                    anchors.centerIn: parent
                    color: "#888888"
                    font.pixelSize: config.fontsize
                }
            }Item{
                height: parent.height
                width: parent.width/6*3
                Text{
                    text: "翻译"
                    anchors.centerIn: parent
                    color: "#888888"
                    font.pixelSize: config.fontsize
                }
            }
        }

    }

    ListView{
        y: 85*dp
        height: root.height - 85*dp
        width: root.width
        model: logModel
        delegate: logDelegate
        clip: true
    }
    ListModel{
        id: logModel
    }Component{
        id: logDelegate
        Rectangle{
            height: 110*dp
            width: root.width
            Rectangle{
                visible: Item1==="" ? true: false
                height: 1
                width: root.width
                color: "#E6E6E6"
                y: parent.height - 1
            }
            Row{
                y: 5*dp
                height: 50*dp;  width: root.width
                Item{
                    height: parent.height
                    width: parent.width/6*2
                    Text{
                        text: Item1
                        width: parent.width
                        anchors.verticalCenter: parent.verticalCenter
                        wrapMode: Text.WordWrap
                        horizontalAlignment: Text.AlignHCenter
                        font.pixelSize: config.fontsize
                    }
                }Item{
                    height: parent.height
                    width: parent.width/6*1
                    Text{
                        text: Item2
                        width: parent.width
                        anchors.verticalCenter: parent.verticalCenter
                        wrapMode: Text.WordWrap
                        horizontalAlignment: Text.AlignHCenter
                        font.pixelSize: config.fontsize
                    }
                }Rectangle{
                    height: parent.height
                    width: parent.width/6*3
                    Text{
                        id:orderinfo
                        width: {
                            var contentW = contentWidth;
                            var maxW = parent.width * 0.8;  // 最大宽度为父容器的80%
                            return Math.min(contentW, maxW);
                        }
                        text: Item3
                        anchors.centerIn: parent
                        font.pixelSize: config.fontsize
                        wrapMode: Text.WordWrap
                        horizontalAlignment: Text.AlignLeft
                    }
                }
            }
            Rectangle {
                height: 50 * dp
                width: parent.width
                y: 55 * dp

                // 鼠标悬停时显示完整宽度
                property bool isHovered: false

                TextInput {
                    id: textItem
                    text: Item4
                    x:20*dp
                    horizontalAlignment: Text.AlignLeft
                    anchors.centerIn: parent
                    font.pixelSize: config.fontsize

                    // 文本选择功能
                    selectByMouse: true
                    readOnly: true

                    // 动态宽度计算
                    width: {
                        // 正常状态：智能选择单行或多行
                        var singleLineWidth = contentWidth;
                        var maxAllowedWidth = parent.width * 0.9;
                        console.log(singleLineWidth,maxAllowedWidth)
                        if (singleLineWidth <= maxAllowedWidth) {
                            wrapMode = Text.NoWrap;
                            return singleLineWidth;
                        } else {
                            wrapMode = Text.WordWrap;
                            return maxAllowedWidth;
                        }
                    }
                }
            }
        }
    }


}
